//
//  TTTableView.m
//  Tint
//
//  Created by Kevin Wu on 2/28/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTTableView.h"

@implementation TTTableView


#pragma mark - Accessors

@synthesize showShadows = _showShadows;

- (void)setShowShadows:(BOOL)showShadows {
    _showShadows = showShadows;
    [self setNeedsLayout];
}



#pragma mark - Memory

- (void)dealloc {
    TTRelease(_originShadowLayer);
    TTRelease(_topShadowLayer);
    TTRelease(_bottomShadowLayer);
    [super dealloc];
}



#pragma mark - UIView

- (void)layoutSubviews {
	[super layoutSubviews];
	
    if (!_showShadows || UITableViewStylePlain != self.style) {
        [_originShadowLayer removeFromSuperlayer];
        [_topShadowLayer removeFromSuperlayer];
        [_bottomShadowLayer removeFromSuperlayer];
        
        TTRelease(_originShadowLayer);
        TTRelease(_topShadowLayer);
        TTRelease(_bottomShadowLayer);
        return ;
    }
    
	// Construct the origin shadow if needed
	if (!_originShadowLayer) {
        CAGradientLayer *shadowLayer = [self shadowAsInverse:NO];
        TTRetain(shadowLayer, _originShadowLayer);
		[self.layer insertSublayer:_originShadowLayer atIndex:0];
	} else if (![[self.layer.sublayers objectAtIndex:0] isEqual:_originShadowLayer]) {
		[self.layer insertSublayer:_originShadowLayer atIndex:0];
	}
	
    // Stretch and place the origin shadow
	[CATransaction begin];
	[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
	_originShadowLayer.frame = TTRectMake(0.0, self.contentOffset.y, self.width, _originShadowLayer.frame.size.height);
	[CATransaction commit];
	
    
    // Remove the table cell shadows if there aren't any cells.
	NSArray *indexPathsForVisibleRows = [self indexPathsForVisibleRows];
	if ([indexPathsForVisibleRows count] == 0) {
		[_topShadowLayer removeFromSuperlayer];
        TTRelease(_topShadowLayer);
        
		[_bottomShadowLayer removeFromSuperlayer];
        TTRelease(_bottomShadowLayer);
        
		return;
	}
	
    
    
    // Assumptions at this point: There are cells.
	NSIndexPath *firstRow = [indexPathsForVisibleRows objectAtIndex:0];
	if (firstRow.section == 0 && firstRow.row == 0) {
		UIView *cell = [self cellForRowAtIndexPath:firstRow];
		if (!_topShadowLayer) {
            CAGradientLayer *shadowLayer = [self shadowAsInverse:YES];
            TTRetain(shadowLayer, _topShadowLayer);
			[cell.layer insertSublayer:_topShadowLayer atIndex:0];
		} else if ([cell.layer.sublayers indexOfObjectIdenticalTo:_topShadowLayer] != 0) {
			[cell.layer insertSublayer:_topShadowLayer atIndex:0];
		}
		_topShadowLayer.frame = TTRectMake(0.0, 0.0 - _topShadowLayer.frame.size.height, cell.width, _topShadowLayer.frame.size.height);
	} else {
		[_topShadowLayer removeFromSuperlayer];
        TTRelease(_topShadowLayer);
	}
    
	NSIndexPath *lastRow = [indexPathsForVisibleRows lastObject];
    // Check whether or not the very last row is visible.
	if (lastRow.section == [self numberOfSections] - 1 &&
		lastRow.row == [self numberOfRowsInSection:lastRow.section] - 1) {
        
		UIView *cell = [self cellForRowAtIndexPath:lastRow];
		if (!_bottomShadowLayer) {
            CAGradientLayer *shadowLayer = [self shadowAsInverse:NO];
            TTRetain(shadowLayer, _bottomShadowLayer);
			[cell.layer insertSublayer:_bottomShadowLayer atIndex:0];
		} else if ([cell.layer.sublayers indexOfObjectIdenticalTo:_bottomShadowLayer] != 0) {
			[cell.layer insertSublayer:_bottomShadowLayer atIndex:0];
		}
		_bottomShadowLayer.frame = TTRectMake(0.0, cell.height, cell.width, _bottomShadowLayer.frame.size.height);
	} else {
		[_bottomShadowLayer removeFromSuperlayer];
        TTRelease(_bottomShadowLayer);
	}
}

@end
